<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Parses input from file according to a format</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="splfileobject.fread.html">« SplFileObject::fread</a></li>
      <li style="float: right;"><a href="splfileobject.fseek.html">SplFileObject::fseek »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.splfileobject.html">SplFileObject</a></li>
    <li>Parses input from file according to a format</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="splfileobject.fscanf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplFileObject::fscanf</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SplFileObject::fscanf</span> &mdash; <span class="dc-title">Parses input from file according to a format</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-splfileobject.fscanf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SplFileObject::fscanf</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&...$vars</code></span>): <span class="type"><span class="type">array</span>|<span class="type">int</span>|<span class="type">null</span></span></div>

  <p class="para rdfs-comment">
   Reads a line from the file and interprets it according to the specified <code class="parameter">format</code>.
  </p>
  <p class="para">
   Any whitespace in the <code class="parameter">format</code> string matches any whitespace in the line from the file.
   This means that even a tab (<code class="literal">\t</code>) in the format string can match a single space character in the input stream.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-splfileobject.fscanf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    

     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">string</code> 的格式解释，在 <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> 的文档中进行了描述，但有以下区别：
       <ul class="simplelist">
        <li class="member">
         函数不支持区域设置。
        </li>
        <li class="member">
         不支持 <code class="literal">F</code>、<code class="literal">g</code>、<code class="literal">G</code> 和 <code class="literal">b</code>。
        </li>
        <li class="member">
         <code class="literal">D</code> 代表十进制数字。
        </li>
        <li class="member">
         <code class="literal">i</code> stands for integer with base detection.
        </li>
        <li class="member">
         <code class="literal">n</code> 代表到目前为止处理的字符数。
        </li>
        <li class="member">
         <code class="literal">s</code> 在任何空白字符处停止阅读。
        </li>
        <li class="member">
         <code class="literal">*</code> 代替 <code class="literal">argnum$</code> 禁止分配此转换规范。
        </li>
       </ul>
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">vars</code></dt>

     <dd>

      <p class="para">
       The optional assigned values.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-splfileobject.fscanf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   If only one parameter is passed to this method, the values parsed will be
   returned as an array. Otherwise, if optional parameters are passed, the
   function will return the number of assigned values. The optional
   parameters must be passed by reference.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-splfileobject.fscanf-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3711">
    <p><strong>示例 #1 <span class="methodname"><strong>SplFileObject::fscanf()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #DD0000">"misc.txt"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$userinfo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fscanf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s&nbsp;%s&nbsp;%s"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$profession</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$countrycode</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$userinfo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Do&nbsp;something&nbsp;with&nbsp;$name&nbsp;$profession&nbsp;$countrycode<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Contents of users.txt</p></div>
    <div class="example-contents">
<div class="txtcode"><pre class="txtcode">javier   argonaut    pe
hiroshi  sculptor    jp
robert   slacker     us
luigi    florist     it</pre>
</div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-splfileobject.fscanf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li class="member"><span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - 返回格式化字符串</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>